package com.draven.mall.controller;

import com.draven.mall.common.ApiResponse;
import com.draven.mall.entity.Merchant;
import com.draven.mall.entity.Product;
import com.draven.mall.service.MerchantService;
import com.draven.mall.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户端商城控制器
 */
@RestController
@RequestMapping("/api/user/shop")
public class UserShopController {

    @Autowired
    private ProductService productService;
    
    @Autowired
    private MerchantService merchantService;

    /**
     * 获取推荐商品列表
     */
    @GetMapping("/recommended-products")
    public ApiResponse getRecommendedProducts(
            @RequestParam(required = false) Integer limit) {
        // 获取推荐商品列表
        List<Product> products = productService.getRecommendedProducts(limit);
        return ApiResponse.success(products);
    }
    
    /**
     * 获取商家详情
     */
    @GetMapping("/{id}")
    public ApiResponse getMerchantDetail(@PathVariable Long id) {
        Merchant merchant = merchantService.getById(id);
        if (merchant != null) {
            Map<String, Object> result = new HashMap<>();
            result.put("id", merchant.getId());
            result.put("shopName", merchant.getShopName());
            result.put("shopLogo", merchant.getShopLogo());
            result.put("address", merchant.getAddress());
            result.put("phone", merchant.getPhone());
            result.put("description", merchant.getDescription());
            result.put("rating", merchant.getRating());
            result.put("businessHours", merchant.getBusinessHours());
            result.put("minPrice", merchant.getMinPrice());
            result.put("deliveryFee", merchant.getDeliveryFee());
            
            return ApiResponse.success(result);
        } else {
            return ApiResponse.error("商家不存在");
        }
    }

    /**
     * 获取商品详情
     */
    @GetMapping("/product/{id}")
    public ApiResponse getProductById(@PathVariable Long id) {
        Product product = productService.getProductById(id);
        if (product != null && product.getStatus() == 1) { // 只返回上架商品
            return ApiResponse.success(product);
        } else {
            return ApiResponse.error("商品不存在或已下架");
        }
    }
    
    /**
     * 搜索商品
     */
    @GetMapping("/products/search")
    public ApiResponse searchProducts(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "12") Integer size) {
        
        // 查询状态为上架的商品
        Integer status = 1;
        
        // 创建结果集
        Map<String, Object> result = new HashMap<>();
        
        // 1. 搜索商品
        Map<String, Object> productResult = productService.getProductPage(null, categoryId, keyword, status, page, size);
        result.put("products", productResult);
        
        // 2. 如果有关键词，也搜索商家
        if (keyword != null && !keyword.isEmpty()) {
            Map<String, Object> params = new HashMap<>();
            params.put("keyword", keyword);
            params.put("page", page);
            params.put("size", size);
            
            Map<String, Object> merchantResult = merchantService.getNearbyShops(null, null, categoryId, keyword, page, size);
            result.put("merchants", merchantResult);
        } else {
            Map<String, Object> emptyResult = new HashMap<>();
            emptyResult.put("records", new ArrayList<>());
            emptyResult.put("total", 0);
            result.put("merchants", emptyResult);
        }
        
        return ApiResponse.success(result);
    }
} 